/**
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package spring.cloud.tasks.tasks_manager.service.impl;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.tasks_manager.database.entity.TaskConfigurationDto;
import spring.cloud.tasks.tasks_manager.database.service.TaskConfigurationDtoService;
import spring.cloud.tasks.tasks_manager.domain.TaskConfiguration;
import spring.cloud.tasks.tasks_manager.exception.ConsoleException;
import spring.cloud.tasks.tasks_manager.service.TaskService;
import spring.cloud.tasks.tasks_manager.service.ZooKeeperService;
import spring.cloud.tasks.tasks_manager.util.BeanUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class TaskServiceImpl implements TaskService {

    public static final String CONFIG_ITEM_LOAD_LEVEL = "loadLevel";
    public static final String CONFIG_ITEM_ENABLED = "enabled";
    public static final String CONFIG_ITEM_DESCRIPTION = "description";
    public static final String CONFIG_ITEM_CUSTOM_CONTEXT = "customContext";
    public static final String CONFIG_ITEM_JOB_TYPE = "jobType";

    public static final String CONFIG_ITEM_SHARDING_ITEM_PARAMETERS = "shardingItemParameters";
    public static final String CONFIG_ITEM_JOB_PARAMETER = "jobParameter";

    public static final String CONFIG_ITEM_FAILOVER = "failover";
    public static final String CONFIG_ITEM_MONITOR_EXECUTION = "monitorExecution";
    public static final String CONFIG_ITEM_TIMEOUT_4_ALARM_SECONDS = "timeout4AlarmSeconds";
    public static final String CONFIG_ITEM_TIMEOUT_SECONDS = "timeoutSeconds";
    public static final String CONFIG_DISABLE_TIMEOUT_SECONDS = "disableTimeoutSeconds";
    public static final String CONFIG_ITEM_TIME_ZONE = "timeZone";
    public static final String CONFIG_ITEM_CRON = "cron";
    public static final String CONFIG_ITEM_PAUSE_PERIOD_DATE = "pausePeriodDate";
    public static final String CONFIG_ITEM_PAUSE_PERIOD_TIME = "pausePeriodTime";
    public static final String CONFIG_ITEM_PROCESS_COUNT_INTERVAL_SECONDS = "processCountIntervalSeconds";
    public static final String CONFIG_ITEM_SHARDING_TOTAL_COUNT = "shardingTotalCount";
    public static final String CONFIG_ITEM_SHOW_NORMAL_LOG = "showNormalLog";
    public static final String CONFIG_ITEM_JOB_DEGREE = "jobDegree";
    public static final String CONFIG_ITEM_ENABLED_REPORT = "enabledReport";
    public static final String CONFIG_ITEM_PREFER_LIST = "preferList";
    public static final String CONFIG_ITEM_USE_DISPREFER_LIST = "useDispreferList";


    public static final String CONFIG_ITEM_JOB_CLASS = "jobClass";
    public static final String CONFIG_ITEM_RERUN = "rerun";


    @Resource
    private ZooKeeperService zooKeeperService;
    @Resource
    private TaskConfigurationDtoService taskConfigurationDtoService;


    @Override
    public List<TaskConfiguration> getTaskConfigurationList(String nameSpace) throws ConsoleException {
        List<TaskConfiguration> taskConfigurationList = new ArrayList<TaskConfiguration>();
        List<TaskConfigurationDto> taskConfiguration4DBList = taskConfigurationDtoService.getTaskConfigurationDtoListByNamespace(nameSpace);
        if (taskConfiguration4DBList != null) {
            for (TaskConfigurationDto taskConfigurationDto : taskConfiguration4DBList) {
                TaskConfiguration taskConfiguration = new TaskConfiguration();
                BeanUtils.copyProperties(taskConfigurationDto, taskConfiguration);
                taskConfigurationList.add(taskConfiguration);
            }
        }
        return taskConfigurationList;
    }


    @Override
    public List<String> getUnSystemJobNames(String nameSpace) throws ConsoleException {
        List<String> taskIdList = new ArrayList<String>();
        List<TaskConfigurationDto> taskConfiguration4DBList = taskConfigurationDtoService.getTaskConfigurationDtoListByNamespace(nameSpace);
        if (taskConfiguration4DBList != null) {
            for (TaskConfigurationDto taskConfiguration4DB : taskConfiguration4DBList) {
                taskIdList.add(taskConfiguration4DB.getTaskId());
            }
        }
        return taskIdList;
    }

    @Override
    public List<String> getTaskIdList(String nameSpace) throws ConsoleException {
        List<String> taskIdList = taskConfigurationDtoService.findConfigNamesByNameSpace(nameSpace);
        return taskIdList != null ? taskIdList : Lists.<String>newArrayList();
    }


}
